package pro.survey.analyzer.lectors.window;

import java.util.List;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pro.survey.analyzer.HibernateUtil;
import pro.survey.analyzer.data.Lectors;

/**
 *
 * @author Adrian
 */
public class LectorsWindow {

    private final static int WINDOW_WIDTH = 300;
    private final static int SP_PANEL_HEIGHT = 250;
    private Stage dialogStage;
    private LectorCheckBox[] cbs;
    private ButtonOK buttonOK;
    private ButtonSelectAll buttonSelectAll;
    private ButtonCancel buttonCancel;
    private static LectorsWindow instance = null;

    /**
     *
     * @return ImportsWindow
     */
    public static LectorsWindow getInstance() {
        if (instance == null) {
            instance = new LectorsWindow();
        }
        return instance;
    }

    private LectorsWindow() {
    }

    public LectorsWindow init(int importId) {
        dialogStage = new Stage();
        buttonOK = new ButtonOK(dialogStage);
        buttonCancel = new ButtonCancel(dialogStage);
        buttonSelectAll = new ButtonSelectAll(dialogStage);
        dialogStage.initModality(Modality.APPLICATION_MODAL);

        List<Lectors> lectors = HibernateUtil.getSession().createQuery(
                "from Lectors lec where lec.imports = ?")
                .setInteger(0, importId)
                .list();
        cbs = new LectorCheckBox[lectors.size()];
        for (int i = 0; i < lectors.size(); i++) {
            cbs[i] = new LectorCheckBox(lectors.get(i).getName().concat(" ").
                    concat(lectors.get(i).getSurname()), lectors.get(i).getId());
        }
        VBox box = VBoxBuilder.create().spacing(10).alignment(Pos.CENTER_LEFT).build();
        box.getChildren().addAll(cbs);
        box.setPadding(new Insets(10));
        ScrollPane sp = new ScrollPane();
        sp.setContent(box);
        sp.setMinHeight(SP_PANEL_HEIGHT);
        sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        VBox mainBox = VBoxBuilder.create().spacing(10).alignment(Pos.CENTER).build();
        HBox hb = HBoxBuilder.create().spacing(10).
                alignment(Pos.CENTER).children(buttonSelectAll, buttonOK, buttonCancel).
                padding(new Insets(10)).build();
        mainBox.getChildren().addAll(sp, hb);
        Scene dialogScene = new Scene(mainBox);
        dialogStage.setScene(dialogScene);
        dialogStage.setWidth(WINDOW_WIDTH);
        dialogStage.show();
        return instance;
    }
    
    public ButtonOK getButtonOK() {
        return buttonOK;
    }
    
    public LectorCheckBox[] getCbs() {
        return cbs;
    }
    
    public void selectAllCheckBox() {
        for(LectorCheckBox cb : cbs) {
            cb.setSelected(true);
        }
        buttonOK.setDisable(false);
    }
    
//
//    public ButtonCancel getButtonCancel() {
//        return buttonCancel;
//    }
}
